package com.lanou.myapp.book.boybook;

/**
 * Created by dllo on 16/1/15.
 */

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.bartoszlipinski.recyclerviewheader.RecyclerViewHeader;
import com.google.gson.Gson;
import com.lanou.myapp.R;
import com.lanou.myapp.imageloader.VolleySingleton;
import com.lanou.myapp.loadpicture.Advertisements;
import com.lanou.myapp.loadpicture.RequestManager;
import com.lanou.myapp.progressdialog.Dialog;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class BoyBookFragment extends Fragment {
    private String url = "http://4g.if.qidian.com/Atom.axd/Api/BookStore/GetBookStoreList?sId=0";
    private Context context;
    private LinearLayout llAdvertiseBoard;
    private LayoutInflater inflater;
    private RecyclerBoyBookAdapter adapter;
    private RecyclerView recyclerView;
    private List<BoyBook> data;
    private RequestQueue queue;
    private Dialog dialog;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        this.context = context;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        RequestManager.init(getActivity());
        inflater = LayoutInflater.from(getActivity());
        recyclerView = (RecyclerView) getActivity().findViewById(R.id.recyclerView_boy_book);
        dialog = new Dialog(context,"dfdfd");
        dialog.setTitle("正在加载，请稍后...");
        dialog.show();

        RecyclerViewHeader header = RecyclerViewHeader.fromXml(context, R.layout.fragment_boy_book_head);
        adapter = new RecyclerBoyBookAdapter(context);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        header.attachTo(recyclerView);

        recyclerView.setAdapter(adapter);
        initViews();



        StringRequest request = new StringRequest(url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                data = new ArrayList<>();
                Gson gson = new Gson();
                GroupBook beans = gson.fromJson(response, GroupBook.class);
                data = beans.getGroup();
                adapter.addData(data);
                dialog.dismiss();
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });

        VolleySingleton.getInstance()._addRequest(request);

    }


    //添加轮播图图片
    private void initViews() {
        llAdvertiseBoard = (LinearLayout) getActivity().findViewById(R.id.llAdvertiseBoard);
        // 添加图片的Url地址
        JSONArray advertiseArray = new JSONArray();
        try {
            JSONObject head_img0 = new JSONObject();
            head_img0.put("head_img", "http://file1.qidian.com/BookStoreRecomm/201601/635881870428482777.jpg");
            JSONObject head_img1 = new JSONObject();
            head_img1.put("head_img", "http://file1.qidian.com/BookStoreRecomm/201601/635881053373538624.jpg");
            JSONObject head_img2 = new JSONObject();
            head_img2.put("head_img", "http://file1.qidian.com/BookStoreRecomm/201601/635881053177112824.jpg");
//            JSONObject head_img3 = new JSONObject();
//            head_img3.put("head_img", "http://img.xiaba.cvimage.cn/4cbc56c1a57e26873c140000.jpg");
            advertiseArray.put(head_img0);
            advertiseArray.put(head_img1);
            advertiseArray.put(head_img2);
//            advertiseArray.put(head_img3);
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        llAdvertiseBoard.addView(new Advertisements(getActivity(), true, inflater, 3000).initView(advertiseArray));
    }


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = LayoutInflater.from(context).inflate(R.layout.fragment_boy_book, null);
        return view;
    }
}
